package com.github.shiqiyue.app.api.sapi.controller.app;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.shiqiyue.app.api.sapi.common.SApiBaseController;
import com.github.shiqiyue.app.common.req.BasePageReq;
import com.github.shiqiyue.app.common.result.Result;
import com.github.shiqiyue.app.common.result.SimplePageResult;
import com.github.shiqiyue.app.modules.app.entity.News;
import com.github.shiqiyue.app.modules.app.enums.NewsStatusEnum;
import com.github.shiqiyue.app.modules.app.service.INewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.text.ParseException;

/***
 * News 接口
 * @author wenyao.wu
 * @since 2020/6/17 0017
 */
@RestController
@RequestMapping("sapi/app/news")
public class SApiNewsController extends SApiBaseController {

    @Autowired
    private INewsService newsService;

    /***
     * 分页
     * @param pageReq
     * @param bindingResult
     * @return
     */
    @PostMapping("page")
    public Result page(@Valid BasePageReq pageReq, BindingResult bindingResult) throws ParseException {
        if (bindingResult.hasErrors()) {
            return Result.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        IPage<News> resultPage = newsService.page(pageReq);
        SimplePageResult<News> result = new SimplePageResult();
        result.setPageInfo(resultPage);
        result.setRecords(resultPage.getRecords());
        return Result.success(result);
    }

    /***
     * 添加
     * @param news
     * @return
     */
    @PostMapping("add")
    public Result add(News news) {
        news.setStatus(NewsStatusEnum.OFF.getValue());
        newsService.save(news);
        return Result.success();
    }

    /***
     * 详情
     * @param id
     * @return
     */
    @PostMapping("detail")
    public Result detail(Long id) {
        Assert.notNull(id);
        News news = newsService.getById(id);
        return Result.success(news);
    }

    /***
     * 修改
     * @param news
     * @return
     */
    @PostMapping("edit")
    public Result edit(News news) {
        newsService.updateById(news);
        return Result.success();
    }

    /***
     * 上架
     * @param id
     * @return
     */
    @PostMapping("on")
    public Result on(Long id) {
        Assert.notNull(id);

        News news = newsService.getById(id);
        news.setStatus(NewsStatusEnum.ON.getValue());
        newsService.updateById(news);

        return Result.success();
    }

    /***
     * 下架
     * @param id
     * @return
     */
    @PostMapping("off")
    public Result off(Long id) {
        Assert.notNull(id);

        News News = newsService.getById(id);
        News.setStatus(NewsStatusEnum.OFF.getValue());
        newsService.updateById(News);

        return Result.success();
    }

}
